package com.ys.mq;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ys.constant.ShopCode;
import com.ys.entity.MQEntity;
import com.ys.mapper.CouponMapper;
import com.ys.pojo.TradeCoupon;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

@Slf4j
@Component
@RocketMQMessageListener(topic = "${mq.order.topic}",
        consumerGroup = "${mq.order.consumer.group.name}",
        messageModel = MessageModel.BROADCASTING)
public class CouponRockerMQListener implements RocketMQListener<MessageExt> {

    @Autowired
    private CouponMapper couponMapper;

    @Override
    public void onMessage(MessageExt message) {

        try {
            //1. 解析消息内容
            String body = new String(message.getBody(), "UTF-8");
            MQEntity mqEntity = JSON.parseObject(body, MQEntity.class);
            log.info("优惠券服务接收到消息");
            //2. 查询优惠券信息
            TradeCoupon coupon = couponMapper.selectById(mqEntity.getCouponId());
            //3.更改优惠券状态
           /* coupon.setUsedTime(null);
            coupon.setIsUsed(ShopCode.SHOP_COUPON_UNUSED.getCode());
            coupon.setOrderId(null);*/
            UpdateWrapper<TradeCoupon> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().set(TradeCoupon::getOrderId,null)
                                    .set(TradeCoupon::getIsUsed,ShopCode.SHOP_COUPON_UNUSED.getCode())
                                    .set(TradeCoupon::getUsedTime,null);
            couponMapper.update(coupon,updateWrapper);
            log.info("回退优惠券成功");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            log.error("回退优惠券失败");
        }

    }
}